package com.example.smart.room.repository

import com.example.smart.room.bean.ReservationBean
import com.example.smart.room.dao.ReservationDAO
import com.example.smart.room.repository.base.BaseRepository

class ReservationRepository(private val reservationDAO: ReservationDAO) : BaseRepository() {

    private suspend fun checkIsExists(date: String, result: suspend (Boolean) -> Unit) {
        result.invoke(reservationDAO.checkIsExists(date).isEmpty())
    }

    fun insert(data: ReservationBean) {
        launch {
            checkIsExists(data.date) {
                if (it) {
                    reservationDAO.insert(data)
                } else {
                    update(data)
                }
            }
        }
    }

    private suspend fun update(data: ReservationBean) {
        reservationDAO.update(data.date, data.time)
    }

    fun getAllData(result: (MutableList<ReservationBean>) -> Unit) {
        launch {
            result.invoke(reservationDAO.getAllData())
        }
    }
}